import TableLyout from "@/components/commonPublic/tableLyoutLong.vue";
import elBreadcrumb from "@/components/public/el-breadcrumb.vue"; //面包屑
import publicForm from "@/components/materialDataManage/fromCom.vue"; //模糊查询
import tree from "@/components/materialQualityPage/sampleManage/tree";
import TableData from "@/components/public/el-table-pagination"; //主页面列表
import CustomDialog from "@/components/materialQualityPage/testDetectionManage/InspectionForm/index.vue";
import CustomDialog1 from "@/components/materialQualityPage/testDetectionManage/InspectionFormText/index.vue";

import formJs from "./regexDetail.js"; //新增弹框配置项

import {
    getUserCompanyTreeList, //左侧机构树接口
    getInspectionFormWebList, //主页面列表接口
    getfSampletype, //获取样品类型
} from "@/api/request.js";
// import { statusType2 } from "@/utils/config"; //配置项
export default {
    name: "InspectionForm",
    components: {
        TableLyout,
        elBreadcrumb, //面包屑
        publicForm, //模糊查询
        tree, //左侧组织机构数
        TableData, //主页面列表
        CustomDialog, //弹框组件
        CustomDialog1,//详情弹框
    },
    data() {
        return {
            name: "InspectionForm", //按钮配置页面代号
            queryCriteriaData: {}, //分页
            btnObj: [], //按钮
            searchObj: formJs.searchObj, //搜索条件

            // 列表
            table_config: formJs.table_config,
            tableData: [], //列表数据列表
            // checked: true, //查看下级按钮
            loading: false, //列表数据加载loading
            getList: {
                fCompanyid: "", // 左侧选中组织机构id
                keyWord: "",// 试验名称、报告编号、工程部位/用途：
                keyType: "",// 检测类型
                modelType: "2", // 0待办 1已办 2查询
                page: 1, // 当前页码
                pageSize: 20, // 每页显示条数
                userId: "", // 查询待办或已办时传 列表页查询不传
                startDates: "", // 开始时间
                endDates: "", // 截至时间
                timeDates: [],
                fSamplenostr:""
            }, // 列表给后端传参
            // 列表

            // 弹框组件
            currentRow: null, //列表详情
            isShow: false, //是否显示弹框
            statusNum: null, //点击那个按钮
            // 弹框组件

            // 详情
            dialogTitle: "",
            statusNum1: null,
            isShow1: false,
            // 详情


            // 左侧机构树
            treelength: false, //左侧树显示隐藏
            showName: "未选择公司",
            treeData: [], //左侧树状图数据
            defaultProps: {
                children: "children",
                label: "fShortname",
            },
            checkData: [],
            treeText: {
                companyId: null,
                text: null,
                userId: null,
            },
            // 左侧机构树

            // 下拉框树
            selectTreeData: [],
            selectCheckData: [],
            selectDefaultProps: {
                children: "children",
                label: "fSampletypename",
            },
            // 下拉框树
            userInfo: {}, //登录用户
        };
    },

    mounted() {
        this.userInfo = JSON.parse(sessionStorage.getItem("yjSmart-userInfo"));

        if (JSON.parse(sessionStorage.getItem("yjSmart-sampleManage_treeCheck"))) {
            var id = JSON.parse(
                sessionStorage.getItem("yjSmart-sampleManage_treeCheck")
            );
            this.checkData = [id.fCompanyid];
        }

        this.getfSampletypeFun();//样品种类
        this.getDate();
        this.getTree();
        this.btnRole(this.name);

        this.table_config.operation.cols = [
            {
                prop: "fStatus",
                type: "primary",
                handleRow: this.handleRow,
            },
        ];
        this.table_config.operation["specialClass"] = function (rowData) {
            var color = null;
            if (rowData == "待报验") {
                color = "red !important";
            } else if (rowData == "审核中") {
                color = "orange !important";
            } else if (rowData == "已报验") {
                color = "green !important";
            } else {
                color = "#409eff !important";
            }
            return color;
        };

    },
    methods: {
        btnRole(name) {
            if (this.userInfo.userId != "System") {
                var btnList = [];
                var ModuleList = JSON.parse(
                    sessionStorage.getItem("yjSmart-ModuleList")
                );
                if (ModuleList.length > 0) {
                    ModuleList.map((item) => {
                        if (item.fEncode == name) {
                            btnList = item.modulebuttonList;
                        }
                    });
                    btnList.map((item) => {
                        item["disabled"] = false;
                    });
                    this.btnObj = btnList;
                }
            } else {
                this.btnObj = formJs.btnObj;
            }
        },
        getBtnName(name, status) {
 
                this.yj_edit(status);
            
        },
        // 左侧树结构
        getTree() {
            this.treeText = {
                companyId: this.userInfo.companyId,
                userId: this.userInfo.userId,
                // text: "4", // 0其他  1 砼拌合站 2水稳 3沥青
            };
            getUserCompanyTreeList(this.treeText)
                .then((res) => {
                    if (res.errcode == 0) {
                        var companyTree = res.data.userCompanyTreeList;
                        this.treeData = this.commonJs.tranListToTreeData(companyTree);
                        if (this.treeData[0].children == undefined) {
                            this.treelength = false; //左侧组织机构是否显示
                        } else {
                            this.treelength = true;
                        }

                        if (this.checkData[0]) {
                            this.showName = JSON.parse(
                                sessionStorage.getItem("yjSmart-sampleManage_treeCheck")
                            ).fShortname;

                            this.getList.fCompanyid = this.checkData[0];
                        } else {
                            this.checkData[0] = this.treeData[0].fCompanyid;
                            this.showName = this.treeData[0].fShortname; //当前点击组织机构名称
                            this.getList.fCompanyid = this.treeData[0].fCompanyid; //当前点击组织机构ID
                        }
                        this.yj_searchForm(this.getList); // 调用主页面列表接口
                        // 列表
                    } else {
                        this.$message.error(res.msg);
                        // this.$message.error("获取组织机构失败");
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 点击左侧机构树
        handleNodeClick(data) {
            this.showName = data.fShortname; //顶部显示机构名
            this.getList.fCompanyid = data.fCompanyid;
            this.getList.page = 1;
            this.getList.pageSize = 20;
            this.yj_searchForm(this.getList);
        },

        // 获取列表数据
        yj_searchForm(data) {
            if (data.keyType == "==请选择==") {
                data.keyType = ""; //样品种类汉字
            }
            // window.sessionStorage.setItem(
            //     "yjSmart-sampleManage_getLists",
            //     JSON.stringify(data)
            // );

            var obj = {
                fCompanyid: data.fCompanyid, //左侧选中组织机构id
                keyWord: data.keyWord,// 试验名称、报告编号、工程部位/用途：
                keyType: data.keyType,// 检测类型
                modelType: "2", // 0待办 1已办 2查询
                page: data.page, // 当前页码
                pageSize: data.pageSize, // 每页显示条数
                userId: "", // 查询待办或已办时传 列表页查询不传
                startDates: data.startDates, // 开始时间
                endDates: data.endDates, // 截至时间
                fSamplenostr:data.fSamplenostr
            };
            this.loading = true;
            var that = this;
            getInspectionFormWebList(obj)
                .then((res) => {
                    if (res.errcode == 0) {
                        that.loading = false;
                        that.tableData = res.data.pageInfo.list;
                        this.queryCriteriaData = res.data.pageInfo;
                    } else {
                        this.$message.error("列表数据获取失败", res.msg);
                        this.loading = false;
                    }
                })
                .catch((error) => {
                    this.$message.error("列表数据 接口出错", error.msg);
                    this.loading = false;
                });
        },
        // 点击当前行
        rowChange(val) {
            this.currentRow = val;
            return val;
        },
        // 查看
        yj_viewForm(row, label) {
            this.currentRow = row;
            if (label == "报验单编号") {
                this.statusNum = 7;
                this.isShow = true;
            }
            if (label == "样品编号") {
                this.yj_edit(7);
            }
            if (label == "报告编号") {
                if (row.fReporturl != "" && row.fReporturl != null && row.fReporturl != "/") {
                    window.open(row.fReporturl);
                } else {
                    this.$message.warning("试验报告暂未出具");
                }
            }

            if (label == "抽检报告编号") {
                if (row.fReportjlurl != "" && row.fReportjlurl != null && row.fReportjlurl != "/") {
                    window.open(row.fReportjlurl);
                } else {
                    this.$message.warning("抽检报告暂未出具");

                }

            }
        },
        // 详情
        yj_edit(num) {
      
            if(num==7){
                if (this.currentRow != null && this.currentRow.fId != "") {
                    this.statusNum1 = 7;
                    this.dialogTitle = "查看";
                    this.isShow1 = true;
                } else {
                    this.$message.warning("请先选择需要查看的数据");
                }
            }else if(num==1){
                if (this.currentRow != null) {
                    if(this.currentRow.fStatus=="已报验"){
                        window.open("http://1.14.103.201:8300/IOTManagement/Web/Sample/getExcelByInspectionForm.do?fId="+this.currentRow.fId);
                    }else {
                        this.$message.warning("请先选择状态为“已报验”的数据");
                    }
                    
                } else {
                    this.$message.warning("请先选择需要打印的数据");

                }
            }
           

        },

        //获取时间
        getDate() {
            // if (JSON.parse(sessionStorage.getItem("yjSmart-sampleManage_getLists"))) {
            //     var dataList = JSON.parse(
            //         sessionStorage.getItem("yjSmart-sampleManage_getLists")
            //     );
            //     this.getList = {
            //         fCompanyid: dataList.fCompanyid, // 左侧选中组织机构id
            //         keyWord: dataList.keyWord,// 试验名称、报告编号、工程部位/用途：
            //         keyType: dataList.keyType,// 检测类型
            //         modelType: "2", // 0待办 1已办 2查询
            //         page: 1, // 当前页码
            //         pageSize: 20, // 每页显示条数
            //         userId: this.userInfo.userId, // 查询待办或已办时传 列表页查询不传
            //         startDates: dataList.startDates, // 开始时间
            //         endDates: dataList.endDates, // 截至时间
            //         timeDates: [dataList.startDates, dataList.endDates],
            //     };
            // } else {
            this.getList.startDates = this.common.getDay(-6); // 开始时间
            this.getList.endDates = this.common.getDay(0); // 截至时间
            this.getList.timeDates = [
                this.getList.startDates,
                this.getList.endDates,
            ];
            // }
        },

        // 弹框组件
        refreshList() {
            //刷新列表
            this.$nextTick(() => {
                this.yj_searchForm(this.getList);
            });
        },
        closeDialog() {
            this.isShow = false;
            this.isShow1 = false;
        },
        // 弹框组件




        // 筛选条件
        // 下拉框树
        getfSampletypeFun() {
            var data = {
                fChecktypenum: "0", //0自检1外委
                fMaterialtypeid: "", //材料进场材料种类id
            };
            getfSampletype(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        // this.selectTreeData = this.tranListToTreeData(
                        //   res.data.SampletypeList
                        // );
                        var arr = [{
                            children: [],
                            fParentId: "0",
                            fSamplemodel: "",
                            fSampletype: "0",
                            fSampletypename: "==请选择=="
                        }];
                        this.selectTreeData = arr.concat(this.tranListToTreeData(
                            res.data.SampletypeList
                        ));

                        this.searchObj[1].tree.treeData = this.selectTreeData;
                        this.searchObj[1].tree.checkData = [
                            this.selectTreeData[0].fSampletype,
                        ];
                    } else {
                        this.$message.error("获取样品种类", res.msg);
                        this.loadingEditDialog_cl = false;
                    }
                })
                .catch((error) => {
                    console.log("获取样品种类 接口出错", error.msg);
                    this.loadingEditDialog_cl = false;
                });
        },
        tranListToTreeData(data) {
            let result = [];
            if (!Array.isArray(data)) {
                return result;
            }
            // 清除已存在的数据  重新赋值
            data.forEach((item) => {
                delete item.children;
            });

            let map = {};
            data.forEach((item) => {
                map[item.fSampletype] = item;
            });

            data.forEach((item) => {
                let parent = map[item.fParentId];
                if (parent) {
                    (parent.children || (parent.children = [])).push(item);
                } else {
                    result.push(item);
                }
            });
            return result;
        },
        selectHandleNodeClick(data) {
            this.getList.keyType = data.fSampletypename; //样品种类汉字
            this.yj_searchForm(this.getList);
        },
        // 下拉框树
        // 时间间隔
        selectTime(e) {
            if (e[0] != "" && e[1] != "") {
                this.getList.startDates = e[0] + " " + "00:00:00"; // 开始时间
                this.getList.endDates = e[1] + " " + "23:59:59"; // 截至时间
            } else {
                this.getList.startDates = ""; // 开始时间
                this.getList.endDates = ""; // 截至时间
            }
            this.getList.timeDates = [e[0], e[1]];
            this.yj_searchForm(this.getList);
        },
        // 筛选条件
    },
};